﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ExchangeListener
{
    class CalendarNotifier
    {
        private ExchangeServiceBinding esb;

        public CalendarNotifier(ExchangeServiceBinding _esb)
        {
            esb = _esb;
            list = new Hashtable();
        }

        private Hashtable list;

        public void DoWork(DateTime StartTime, DateTime EndTime)
        {
            AppointmentsListener.Instance.Listener(StartTime, EndTime, esb, list);

            RecurringAppointmentListener.Instance.Listener(StartTime, EndTime, esb, list);

            Notify();
        }

        private void Notify()
        {
            DateTime now = DateTime.Now;
            if (list.Count > 0)
                foreach (var item in list.Values)
                {
                    Appointment ap = (Appointment)item;
                    if (now > ap.Start)
                    {
                        list.Remove(ap.ID);
                    }
                    else if (now > ap.RemindTime && ap.IsNotified == false)
                    {
                        SendSMS.Instance.Send(ap);
                        //list.Remove(ap.ID);
                        ap.IsNotified = true;
                        if (list.Count == 0)
                            break;
                    }
                   // Console.WriteLine("-----");
                   // SendSMS.Instance.Send(ap);

                    //else
                    //    Console.WriteLine("lower");
                }
        }

    }
}
